//
//  JShareSheetView.m
//  iBuy
//
//  Created by junFung on 15/10/11.
//  Copyright (c) 2015年 yc. All rights reserved.
//

#import "JShareSheetView.h"
CGFloat const JShareSheetViewHeight = 220.0;
@interface JShareSheetView ()
{
    UIView * __viewBg ;
}
@end

@implementation JShareSheetView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        self.alpha = 0.0;
        [WINDOW addSubview:self];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(JShareSheetViewOnTapViewBg)];
        [self addGestureRecognizer:tap];
        
        [self setupJShareSheetViewSubViews];
    }
    return self;
}

- (void)setupJShareSheetViewSubViews
{
    __viewBg= [[UIView alloc] initWithFrame:CGRectMake(0, screenH, screenW, JShareSheetViewHeight)];
    __viewBg.backgroundColor = [UIColor whiteColor];
    [self addSubview:__viewBg];
    
    NSArray *imageArray = @[@"icon_invite_share_logo_weibo",@"icon_invite_share_logo_weixin",@"icon_invite_share_logo_weixin_Friends",@"icon_invite_share_logo_QQ",@"icon_invite_share_logo_Qzone",@"icon_invite_share_logo_sms",];
    NSArray *titleArray = @[@"新浪微博",@"微信",@"微信朋友圈",@"QQ好友",@"QQ空间",@"短信",];
    
    for (int i = 0 ; i < 6; i ++) {
        int X = i%4;
        int Y = i/4;
        
        CGFloat labelMarginY = 5.0;
        CGFloat labelH = 15.0;
        CGFloat buttonW = 60.0;
        
        CGFloat buttonMarginY = 20.0;
        CGFloat buttonMarginX = (screenW-buttonW*4)/5.0;
        
        CGFloat buttonX = (X*(buttonMarginX+buttonW)+buttonMarginX);
        CGFloat buttonY = (Y*(buttonW+buttonMarginY+labelH+labelMarginY)+buttonMarginY);
        
        CGRect buttonFrame = CGRectMake(buttonX, buttonY, buttonW, buttonW);
        UIButton *buttonShare = [[UIButton alloc] initWithFrame:buttonFrame];
        [buttonShare setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [buttonShare addTarget:self action:@selector(onClickButtonShare:) forControlEvents:UIControlEventTouchUpInside];
        [__viewBg addSubview:buttonShare];
        
        CGFloat labelY = Y*((buttonW+buttonMarginY+labelMarginY)+labelH)+(buttonW+buttonMarginY+labelMarginY);
        CGRect labelFrame = CGRectMake(buttonX, labelY, buttonW, labelH);
        UILabel *labelTitle = [[UILabel alloc] initWithFrame:labelFrame];
        labelTitle.text = titleArray[i];
        labelTitle.font = [UIFont systemFontOfSize:12.0];
        labelTitle.textColor = [UIColor darkGrayColor];
        labelTitle.textAlignment = NSTextAlignmentCenter;
        [__viewBg addSubview:labelTitle];
        
        if( i==0) buttonShare.tag = JShareButtonTypeSina;
        if( i==1) buttonShare.tag = JShareButtonTypeWeiXin;
        if( i==2) buttonShare.tag = JShareButtonTypeWXFriend;
        if( i==3) buttonShare.tag = JShareButtonTypeQQFriend;
        if( i==4) buttonShare.tag = JShareButtonTypeQQZone;
        if( i==5) buttonShare.tag = JShareButtonTypeSMS;
        
    }
}

- (void)onClickButtonShare:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(jShareSheetView:clickButtonAtIndex:)]) {
        [self.delegate jShareSheetView:self clickButtonAtIndex:sender.tag];
    }
}

- (void)JShareSheetViewOnTapViewBg
{
    [self hide];
}

- (void)show{
    [UIView animateWithDuration:0.4 animations:^{
        __viewBg.frame = CGRectMake(0, screenH-JShareSheetViewHeight, screenW, JShareSheetViewHeight);
        self.alpha = 1.0;
    }];
}

- (void)hide
{
    [UIView animateWithDuration:0.4 animations:^{
        __viewBg.frame = CGRectMake(0, screenH, screenW, JShareSheetViewHeight);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
